package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Lists;
import com.sc.nft.dao.PledgeTaskCollectionDao;
import com.sc.nft.dao.PledgeTaskRewardDao;
import com.sc.nft.dao.SecondCollectionConfigDao;
import com.sc.nft.entity.*;
import com.sc.nft.entity.vo.AdminPledgeTaskCollectionVO;
import com.sc.nft.entity.vo.AdminPledgeTaskRewardVO;
import com.sc.nft.entity.vo.PledgeTaskCollectionVO;
import com.sc.nft.entity.vo.PledgeTaskRewardVO;
import com.sc.nft.enums.MarketTypeEnum;
import com.sc.nft.enums.PledgeStatusEnum;
import com.sc.nft.service.SecondCollectionConfigService;
import com.sc.nft.service.UserCollectionService;
import com.sc.nft.service.UserPledgeCollectionRecordsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.service.PledgeTaskCollectionService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 质押任务-质押的NFT服务接口实现
 *
 * @author Sun
 * @since 2023-06-12 15:36:26
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class PledgeTaskCollectionServiceImpl implements PledgeTaskCollectionService {
    private final PledgeTaskCollectionDao pledgeTaskCollectionDao;
    private final PledgeTaskRewardDao pledgeTaskRewardDao;
    private final UserPledgeCollectionRecordsService userPledgeCollectionRecordsService;
    private final UserCollectionService userCollectionService;
    private final SecondCollectionConfigDao secondCollectionConfigDao;
    @Override
    public List<AdminPledgeTaskCollectionVO> adminGetListByPledgeTaskId(Long pledgeTaskId) {

        List<AdminPledgeTaskCollectionVO> adminPledgeTaskCollectionVOList = Lists.newArrayList();

        List<PledgeTaskCollection> list = pledgeTaskCollectionDao.getListByPledgeTaskId(pledgeTaskId);
        for (PledgeTaskCollection pledgeTaskCollection : list) {
            //拷贝质押任务
            AdminPledgeTaskCollectionVO adminPledgeTaskCollectionVO = BeanUtil.copyProperties(pledgeTaskCollection, AdminPledgeTaskCollectionVO.class);
            //获取奖励列表
            List<PledgeTaskReward>  pledgeTaskRewardList = pledgeTaskRewardDao.getListByPledgeTaskIdAndPledgeTaskCollectionId(pledgeTaskId,pledgeTaskCollection.getId());
            //拷贝奖励列表
            adminPledgeTaskCollectionVO.setPledgeTaskRewardList(BeanUtil.copyToList(pledgeTaskRewardList, AdminPledgeTaskRewardVO.class));
            adminPledgeTaskCollectionVOList.add(adminPledgeTaskCollectionVO);
        }
        return adminPledgeTaskCollectionVOList;
    }

    @Override
    public List<PledgeTaskCollectionVO> getListByPledgeTaskId(UserInfo user, PledgeTaskInfo pledgeTaskInfo) {
        List<PledgeTaskCollectionVO> pledgeTaskCollectionVOS = Lists.newArrayList();

        List<PledgeTaskCollection> list = pledgeTaskCollectionDao.getListByPledgeTaskId(pledgeTaskInfo.getId());


        for (PledgeTaskCollection pledgeTaskCollection : list) {
            //拷贝质押任务
            PledgeTaskCollectionVO pledgeTaskCollectionVO = BeanUtil.copyProperties(pledgeTaskCollection, PledgeTaskCollectionVO.class);
            pledgeTaskCollectionVO.setPledgeTaskCollectionId(pledgeTaskCollection.getId());
            pledgeTaskCollectionVO.setPledgeTaskName(pledgeTaskInfo.getPledgeTaskName());
            pledgeTaskCollectionVO.setPledgeTaskId(pledgeTaskInfo.getId());

            if(pledgeTaskInfo.getIsRepeatedly()){//可以重复质押
                /**
                 *   可以重复质押
                 *   如果已经质押了  判断剩余数量是否满足质押条件  满足则显示去质押 否则显示去获取
                 *   如果没有质押   则显示去质押
                 *   如果持有藏品 并且没有质押  则显示去质押
                 */


                int holdCount = userCollectionService.getIsContainsAndIsHoldAndIsOpenAndOnConsignByCollectionId(pledgeTaskCollection.getCollectionId(), user.getId());
                if(holdCount < pledgeTaskCollection.getPledgeCount()){//去获取
                    //质押状态 去获取
                    pledgeTaskCollectionVO.setIsPledge(3);
                    //二级配置id
                    SecondCollectionConfig secondCollectionConfig = secondCollectionConfigDao.getByCollectionIdAndMarketType(pledgeTaskCollection.getCollectionId(), MarketTypeEnum.FREE_MARKET);
                    pledgeTaskCollectionVO.setSecondCollectionConfigId(ObjectUtil.isNull(secondCollectionConfig) ? 0L : secondCollectionConfig.getId());
                }else{
                    pledgeTaskCollectionVO.setIsPledge(1);
                }

            }else{//
                /**
                 *   只能质押一次
                 *   如果已经质押了  则显示挖宝中
                 *   如果没有质押   则显示去质押
                 *   如果持有藏品 并且没有质押  则显示去质押
                 */
                //用户持有的藏品数量
                int holdCount = userCollectionService.getIsContainsAndIsHoldAndIsOpenAndOnConsignByCollectionId(pledgeTaskCollection.getCollectionId(), user.getId());
                if(holdCount < pledgeTaskCollection.getPledgeCount()){//去获取
                    //质押状态
                    pledgeTaskCollectionVO.setIsPledge(3);
                    //二级配置id
                    SecondCollectionConfig secondCollectionConfig = secondCollectionConfigDao.getByCollectionIdAndMarketType(pledgeTaskCollection.getCollectionId(), MarketTypeEnum.FREE_MARKET);
                    pledgeTaskCollectionVO.setSecondCollectionConfigId(ObjectUtil.isNull(secondCollectionConfig) ? 0L : secondCollectionConfig.getId());
                }else{
                    pledgeTaskCollectionVO.setIsPledge(1);
                }

                //用户质押记录
                Integer count = userPledgeCollectionRecordsService.getCountByUserIdAndPledgeTaskIdAndCollectionIdAndStatus(user.getId(), pledgeTaskInfo.getId(),pledgeTaskCollection.getId(), PledgeStatusEnum.IN_PLEDGE);
                if(count>=1 && pledgeTaskCollectionVO.getIsPledge()!=3){//挖宝中
                    pledgeTaskCollectionVO.setIsPledge(2);
                }

                if(holdCount >= pledgeTaskCollection.getPledgeCount() && count==0){//去质押
                    pledgeTaskCollectionVO.setIsPledge(1);
                }

            }

            //拷贝奖励列表
            pledgeTaskCollectionVO.setPledgeTaskRewardList(pledgeTaskRewardDao.getListByPledgeTaskIdAndPledgeTaskCollectionId(pledgeTaskInfo.getId(),pledgeTaskCollection.getId()));
            pledgeTaskCollectionVOS.add(pledgeTaskCollectionVO);
        }

        return pledgeTaskCollectionVOS;
    }

    @Override
    public List<PledgeTaskCollection> getListByPledgeTaskId(Long pledgeTaskId) {
        return pledgeTaskCollectionDao.getListByPledgeTaskId(pledgeTaskId);
    }

    @Override
    public PledgeTaskCollection getOneByPledgeTaskIdAndId(Long pledgeTaskId, Long pledgeTaskCollectionId) {
        return pledgeTaskCollectionDao.getOneByPledgeTaskIdAndId(pledgeTaskId,pledgeTaskCollectionId);
    }

    @Override
    public PledgeTaskCollection getOneByPledgeTaskIdAndCollectionId(Long pledgeTaskId, Long pledgeTaskCollectionId) {
        return pledgeTaskCollectionDao.getOneByPledgeTaskIdAndCollectionId(pledgeTaskId,pledgeTaskCollectionId);
    }
}